{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，我是 张三，我今年 25 岁。\n",
      "你好，我是 李四，我今年 30 岁。\n",
      "你好，我是 张三，我今年 26 岁。\n"
     ]
    }
   ],
   "source": [
    "class Person:\n",
    "    # 构造函数（初始化方法）\n",
    "    def __init__(self, name, age):\n",
    "        # 实例变量\n",
    "        self.name = name  # 每个实例都会有自己独立的 name\n",
    "        self.age = age    # 每个实例都会有自己独立的 age\n",
    "\n",
    "    # 实例方法\n",
    "    def greet(self):\n",
    "        print(f\"你好，我是 {self.name}，我今年 {self.age} 岁。\")\n",
    "    \n",
    "    # 获取年龄的方法\n",
    "    def get_age(self):\n",
    "        return self.age\n",
    "    \n",
    "    # 设置年龄的方法\n",
    "    def set_age(self, new_age):\n",
    "        self.age = new_age\n",
    "\n",
    "# 创建 Person 类的实例\n",
    "person1 = Person(\"张三\", 25)\n",
    "person2 = Person(\"李四\", 30)\n",
    "\n",
    "# 调用实例方法\n",
    "person1.greet()  # 输出：你好，我是 张三，我今年 25 岁。\n",
    "person2.greet()  # 输出：你好，我是 李四，我今年 30 岁。\n",
    "\n",
    "# 修改 person1 的年龄\n",
    "person1.set_age(26)\n",
    "person1.greet()  # 输出：你好，我是 张三，我今年 26 岁。\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
